package com.miplus.ccrm.core.service.aiqm;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.core.controller.vo.aiqm.QmModelRulesAddVo;
import com.miplus.ccrm.core.controller.vo.aiqm.QmModelSubAddVo;
import com.miplus.ccrm.core.controller.vo.aiqm.QmModelSubEditVo;
import com.miplus.ccrm.core.mapper.QmQaMainMapper;
import com.miplus.ccrm.core.pojo.QmModelRulesLink;
import com.miplus.ccrm.core.pojo.QmQaMain;
import com.miplus.ccrm.core.pojo.QmQaSub;
import com.miplus.ccrm.core.mapper.QmQaSubMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @author 20144
 * @description 针对表【QM_QA_SUB(质检项子项表)】的数据库操作Service实现
 * @createDate 2025-08-20 13:34:58
 */
@Service
public class QmQaSubServiceImpl extends ServiceImpl<QmQaSubMapper, QmQaSub>
        implements QmQaSubService {
    @Resource
    private QmQaMainMapper qmQaMainMapper;

    @Override
    public CmdResVo qmModelSubRulesAdd(String usrid, QmModelSubAddVo paras) {
        QmQaSub def = new QmQaSub();
        BeanUtils.copyProperties(paras, def);
        def.setCrusr(usrid);
        def.setCrdt(new Date());
        def.setMdusr(usrid);
        def.setMddt(new Date());
        int ret = baseMapper.insert(def);
        if (ret <= 0) {
            return new CmdResVo(ret, "新增质检子项失败");
        }
        //新增成功的执行,新增向质检主项补充质检子项id，主项是数组格式，这里要处理
        Integer qamainid = paras.getQamainid();
        if (qamainid != null) {
            QmQaMain qaMain = qmQaMainMapper.selectById(qamainid);
            if (qaMain != null) {
                // 1. 获取主项中已有的子项ID数组
                String existingSubIdsJson = qaMain.getQasubid(); // 字段名是Qasubid,存储JSON数组
                JSONArray subIdsArray = new JSONArray();
                // 2. 如果已有数据，解析为JSON数组；否则创建新数组
                if (StringUtils.isNotBlank(existingSubIdsJson)) {
                    subIdsArray = JSONUtil.parseArray(existingSubIdsJson);
                }
                // 3. 避免重复添加相同的子项ID
                if (!subIdsArray.contains(def.getQasubid().toString())) {
                    subIdsArray.add(def.getQasubid().toString()); // 存入字符串类型，保持JSON格式统一
                }

                // 4. 将更新后的JSON数组写回主表
                qaMain.setQasubid(subIdsArray.toString());
                qaMain.setMdusr(usrid);
                qaMain.setMddt(new Date());
                qmQaMainMapper.updateById(qaMain);
            }
        }
        return new CmdResVo(ret, def.getQasubid().toString());
    }

    /**
     * 质检子项编辑
     *
     * @param usrid
     * @param paras
     * @return
     */
    @Override
    public CmdResVo qmModelSubRulesEdit(String usrid, QmModelSubEditVo paras) {
        Integer templateid = paras.getTemplateid();
        Long qamainsid = paras.getQamainsid();
        Long qasubid = paras.getQasubid();
        String qasubname = paras.getQasubname();
        BigDecimal maxscore = paras.getMaxscore();
        BigDecimal minscore = paras.getMinscore();
        String qmrole = paras.getQmrole();
        String remark = paras.getRemark();
        String assessscheme = paras.getAssessscheme();
        String qmrange = paras.getQmrange();
        String mustcontain = paras.getMustcontain();
        String mustnotcontain = paras.getMustnotcontain();
        // 更新子项，校验参数
        if (ObjUtil.isNull(qasubid)) {
            return new CmdResVo(0, "质检子项ID不能为空");
        }
        QmQaSub qaSub = new QmQaSub();
        qaSub.setQasubid(qasubid);
        qaSub.setQasubname(qasubname);
        qaSub.setMaxscore(maxscore);
        qaSub.setMinscore(minscore);
        qaSub.setQmrole(qmrole);
        qaSub.setRemark(remark);
        qaSub.setAssessscheme(assessscheme);
        qaSub.setQmrange(qmrange);
        qaSub.setMustcontain(mustcontain);
        qaSub.setMustnotcontain(mustnotcontain);
        qaSub.setMdusr(usrid);
        qaSub.setMddt(new Date());
        int ret = baseMapper.updateById(qaSub);
        if (ret <= 0) {
            return new CmdResVo(ret, "更新质检子项失败");
        }
        return new CmdResVo(ret, qaSub.getQasubid().toString());
    }
}




